package com.common.controller.role;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.entity.Message;
import com.common.entity.Role;
import com.common.entity.RoleCorrelationPermission;
import com.common.service.PermissionService;
import com.common.service.RoleService;

@Controller
public class RoleController {
	
	@Resource
	private RoleService roleService;
	
	@Resource
	private PermissionService permissionService;
	@RequestMapping(value="role",method=RequestMethod.GET)
	public String getRole(Model model){
		model.addAttribute("roleList", roleService.getAllRoleList());
		model.addAttribute("permissionList", permissionService.getAllPermissions());
		return "permission/role";
	}
	
	@ResponseBody
	@RequestMapping(value="addPermission",method=RequestMethod.POST)
	public Message addPermission(String role,Integer permission){
		Role roleStore = new Role();
		roleStore.setRole(role);
		roleStore = roleService.getRoleByName(roleStore);
		RoleCorrelationPermission rolePermission = new RoleCorrelationPermission(roleStore.getId(),permission);
		roleService.correlationPermission(rolePermission);
		Message message = new Message();
		message.setMessage("添加成功");
		return message;
	}
	
	@ResponseBody
	@RequestMapping(value="deletePermission",method=RequestMethod.POST)
	public Message deletePermission(String role,Integer permission){
		Role roleStore = new Role();
		roleStore.setRole(role);
		roleStore = roleService.getRoleByName(roleStore);
		RoleCorrelationPermission rolePermission = new RoleCorrelationPermission(roleStore.getId(),permission);
		roleService.uncorrelationPermission(rolePermission);
		Message message = new Message();
		message.setMessage("删除成功");
		return message;
	}
	
	@ResponseBody
	@RequestMapping(value="/test",method=RequestMethod.GET)
	public Long test(){
		
		return 10000l;
	}
}
